
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import createPersistedState from 'vuex-persistedstate'
export default new Vuex.Store({
    state: {
        CurUser: {},
        menuList: [],
        hasRoutes: false,
        permList: [],//权限
        tab: "Home",
        tabs: [
            {
                title: '系统首页',
                name: 'Home'
            }
        ],
    },
    mutations: {
        setUser(state, user) {
            state.CurUser = user
            localStorage.setItem('CurUser', JSON.stringify(user))
        },
        setMenuList(state, menuList) {
            state.menuList = menuList
            localStorage.setItem('menu', JSON.stringify(menuList))
        },
        setPermList(state, permList) {
            state.permList = permList
            localStorage.setItem('perm', JSON.stringify(permList))
        },
        changeRoutes(state, hasRoutes) {
            state.hasRoutes = hasRoutes
        },
        resetState(state){
            state.CurUser = {}
            localStorage.removeItem('CurUser')
            localStorage.removeItem('token')
            localStorage.removeItem('menu')
            localStorage.removeItem('perm')
            state.menuList = []
            state.permList = []
            state.hasRoutes = false
            state.tab="Home",
            state.tabs = [
                { title: '系统首页', name: 'Home'}
            ]
        },
        addTab(state, tab) {
            let index = state.tabs.findIndex(item => item.name === tab.path)
            console.log(index)
            if (index === -1) {
                state.tabs.push({
                    title: tab.name,
                    name: tab.path
                })
            }
            state.tab=tab.path
        },
    }, getters: {
        getCurUser(state) {
            return state.CurUser
        },
        getMenu(state) {
            return state.menu
        },  
        hasAuth(state) {
            return function (perm) {
                return state.permList.includes(perm)
                // console.log(perm)
                // return state.permList!=null
            }
        },
    },
    plugins: [
        createPersistedState()
    ]
})